package com.yyf.springboot2.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yyf.springboot2.common.ResultBean;
import com.yyf.springboot2.entity.TOrder;
import com.yyf.springboot2.entity.TOrderlist;
import com.yyf.springboot2.entity.TUser;
import com.yyf.springboot2.mapper.TOrderMapper;
import com.yyf.springboot2.mapper.TOrderlistMapper;
import com.yyf.springboot2.service.TGoodsService;
import com.yyf.springboot2.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyf.springboot2.service.TOrderlistService;
import com.yyf.springboot2.service.TUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyf
 * @since 2019-05-29
 */
@Service
@Transactional

public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Resource
    private  TOrderMapper orderMapper;
    @Resource
    private TOrderService orderService;
    @Resource
    private TGoodsService goodsService;
    @Resource
    private TOrderlistMapper orderlistMapper;
    @Resource
    private TOrderlistService orderlistService;
    @Resource
    private TUserService userService;

    @Override
    public Integer saveOrder(TOrder order, List <TOrderlist> orderlists){
        try {
            orderService.save(order);
            System.out.println(order.getId());
            for (int i = 0; i <orderlists.size() ; i++) {
                orderlists.get(i).setOrderId(order.getId());
            }
            orderlistService.saveBatch(orderlists);
            return  1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public List <TOrder> getListByTypeAdUser(Integer id, Integer type, Integer page, Integer pageSize) {
        Map<String,Object> map = new HashMap <>();
        map.put("userId",id);
        map.put("stat",type);//这里的type可以是订单状态，也可以是订单类型
        map.put("page",page);
        map.put("pageSize",pageSize);


        return orderMapper.findListByTypeAdUser(map);
    }

    @Override
    public Integer payOrder(Integer userId, Integer orderId) {

        TUser user = userService.getUserAmount(userId);
        TOrder order = orderService.getById(orderId);
        if(user.getMoneyMy() < order.getTotalPrice()){
        }
        Map<String,Object> map = new HashMap <>();
        map.put("userId",user.getId());
        map.put("moneyMy",order.getTotalPrice());
        Integer result  = userService.updateMoneyById(map);

        map.clear();
        map.put("orderId",orderId);
        map.put("stat",1);
         int re =   orderMapper.updateOrder(map);
          if(re > 0 &&result> 0){
              return 1;
          }




        return -1;
    }
}
